#include <stdio.h>
#include "StdThread.h"
#include <stdlib.h>
#include <pthread.h>

Mutex *m1;
Mutex *m2;

void * func1(void *arg)
{
    MutexLock(m1);
    printf("func1 上锁 m1 \n");
    sleep(1);

    if (MutexTryLock(m2, 1, 1) == false)
    {
        printf("尝试上锁m2失败,放开m1\n");
        MutexUnlock(m1);
        return NULL;
    }
    printf("func2 上锁 m2 \n");

    return NULL;
}

void * func2(void *arg)
{
    MutexLock(m2);
    printf("func2 上锁 m2 \n");
    sleep(1);

    if (MutexTryLock(m1, 1, 1) == false)
    {
        printf("尝试上锁m1失败,放开m2\n");
        MutexUnlock(m2);
        return NULL;
    }
    printf("func1 上锁 m1 \n");

    return NULL;
}

int main()
{
    m1 = MutexInit();
    m2 = MutexInit();

    Thread * t1 = ThreadInit(func1, NULL);
    Thread * t2 = ThreadInit(func1, NULL);


    ThreadJoin(t1);
    ThreadJoin(t2);
    ClearThread(t1);
    ClearThread(t2);
    ClearMutex(m1);
    ClearMutex(m2);

    return 0;
}